﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using System.Text;
using System.IO;
using exel = Microsoft.Office.Interop.Excel;
using System.Diagnostics;
using VietKinsNutrition.Data;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.BandedGrid;
//using DevExpress.XtraGrid.Views.Grid;
//using DevExpress.XtraEditors;
//using DevExpress.XtraGrid.Views.BandedGrid;

namespace VietKinsNutrition.Bussiness
{
    public class ClsKhauphandinhduongtrongtuanBLL : ClsExel
    {

        ClsKhauphandinhduongtrongtuanDAL ClsKhauphandinhduongtrongtuan_dal = new ClsKhauphandinhduongtrongtuanDAL();
        ClsTblDm_SosanhnangluonDAL ClsTblDm_Sosanhnangluon_dal = new ClsTblDm_SosanhnangluonDAL();

        private string[] _Time = new string[9];
        private DateTime _StartDay;
        private DateTime _FinishDay;
        private int _tongSoTre;
        private string _Begin;
        private string _End;
        private DataGridView _GridView;
        private DataTable _Source = new DataTable();
        private DataTable TableTH = new DataTable();

        public ClsKhauphandinhduongtrongtuanBLL()
        {
            _Source.Columns.Add("STT");
            _Source.Columns.Add("Tenthucan");
            _Source.Columns.Add("Tendonvitinh");
            _Source.Columns.Add("day1");
            _Source.Columns.Add("day2");
            _Source.Columns.Add("day3");
            _Source.Columns.Add("day4");
            _Source.Columns.Add("day5");
            _Source.Columns.Add("day6");
            _Source.Columns.Add("tong");
            _Source.Columns.Add("nangluong");
            _Source.Columns.Add("DV1");
            _Source.Columns.Add("TV1");
            _Source.Columns.Add("DV2");
            _Source.Columns.Add("TV2");
            _Source.Columns.Add("duongbot");

            TableTH.Columns.Add("Tieude");
            TableTH.Columns.Add("Ngangluong");
            TableTH.Columns.Add("dam");
            TableTH.Columns.Add("beo");
            TableTH.Columns.Add("duong");
        }

        public DataGridView GridView
        {
            set { _GridView = value; }
        }
        public int TongSoTre
        {
            get { return _tongSoTre; }
        }
        public string Begin
        { get { return _Begin; } }
        public string End
        { get { return _End; } }
        public DateTime StartDay
        {
            get { return _StartDay; }
        }
        public DateTime FinishDay
        { get { return _FinishDay; } }

        public void GetMonth(ComboBox MonthFrom, ComboBox DayFrom, TextBox ToMonth, TextBox ToDay)
        {
            int _Month = 0, _Day = 0, _year = 0;
            string[] _Temp = MonthFrom.Text.Split('/');
            _Month = int.Parse(_Temp[0]); _Day = int.Parse(DayFrom.Text); _year = int.Parse(_Temp[1]);
            int i = 0;
            while (i < 6)
            {
                if (_Day >= this.DayofMonth(_Month, _year))
                {
                    _Day = 1;
                    _Month = _Month + 1;
                    if (_Month > 12)
                    {
                        _Month = 1;
                        _year = _year + 1;
                    }
                    i = i + 1;
                }
                else
                {
                    _Day = _Day + 1;
                    i = i + 1;
                }

            }
            ToMonth.Text = _Month.ToString() + "/" + _year;
            ToDay.Text = _Day.ToString();
        }

        public void GetMonth(DateTime StartDay, AdvBandedGridView gridView, DataGridView gridTemp)
        {
            ReturnDay(StartDay);
            DateTime dayShow = StartDay;
            while (dayShow.DayOfWeek != DayOfWeek.Monday)
                dayShow = dayShow.AddDays(-1);
            for (int i = 0; i < 6; i++)
            {
                _Source.Columns[(i + 3)].ColumnName = dayShow.Day.ToString() + "/" + dayShow.Month.ToString();
                gridView.Columns[(i + 3)].FieldName = dayShow.Day.ToString() + "/" + dayShow.Month.ToString();
                gridTemp.Columns[(i + 3)].DataPropertyName = dayShow.Day.ToString() + "/" + dayShow.Month.ToString();
                gridView.Bands[(i + 3)].Caption =
                    (dayShow.DayOfWeek == DayOfWeek.Monday ? "Thứ 2" :
                    dayShow.DayOfWeek == DayOfWeek.Tuesday ? "Thứ 3" :
                    dayShow.DayOfWeek == DayOfWeek.Wednesday ? "Thứ 4" :
                    dayShow.DayOfWeek == DayOfWeek.Thursday ? "Thứ 5" :
                    dayShow.DayOfWeek == DayOfWeek.Friday ? "Thứ 6" :
                    dayShow.DayOfWeek == DayOfWeek.Saturday ? "Thứ 7" : "CN") + " (" + dayShow.Day.ToString() + "/" + dayShow.Month.ToString() + ")";
                gridTemp.Columns[(i + 3)].HeaderText =
                    (dayShow.DayOfWeek == DayOfWeek.Monday ? "Thứ 2" :
                    dayShow.DayOfWeek == DayOfWeek.Tuesday ? "Thứ 3" :
                    dayShow.DayOfWeek == DayOfWeek.Wednesday ? "Thứ 4" :
                    dayShow.DayOfWeek == DayOfWeek.Thursday ? "Thứ 5" :
                    dayShow.DayOfWeek == DayOfWeek.Friday ? "Thứ 6" :
                    dayShow.DayOfWeek == DayOfWeek.Saturday ? "Thứ 7" : "CN") + " (" + dayShow.Day.ToString() + "/" + dayShow.Month.ToString() + ")";
                dayShow = dayShow.AddDays(1);
            }
            _Begin = _Time[0]; _End = _Time[5];
            string[] _Temp;
            _Temp = _Time[0].Split('/');
            _StartDay = DateTime.Parse(_Temp[1] + "/" + _Temp[0]);
            _Temp = _Time[5].Split('/');
            _FinishDay = _StartDay.AddDays(5); //DateTime.Parse(_Temp[1] + "/" + _Temp[0]);
        }
        public void GetMonth(DateTime StartDay, DateTime FinishDay, AdvBandedGridView gridView, DataGridView gridTemp)
        {
            _StartDay = StartDay;
            _FinishDay = FinishDay;
            int j = 0;
            int cSunday = 0;
            for (int i = 0; i < 6; i++)
            {
                DateTime dayShow = StartDay.AddDays(i + cSunday);
                if (dayShow.DayOfWeek == DayOfWeek.Sunday)
                {
                    cSunday += 1;
                    dayShow = dayShow.AddDays(1);
                }
                //gridView.Columns[(i + 3)].Name = dayShow.Day.ToString() + "/" + dayShow.Month.ToString();
                //gridView.Columns[(i + 3)].HeaderText =
                _Source.Columns[(i + 3)].ColumnName = dayShow.Day.ToString() + "/" + dayShow.Month.ToString();
                gridView.Columns[(i + 3)].FieldName = dayShow.Day.ToString() + "/" + dayShow.Month.ToString();
                gridTemp.Columns[(i + 3)].DataPropertyName = dayShow.Day.ToString() + "/" + dayShow.Month.ToString();
                gridView.Bands[(i + 3)].Caption =
                    (dayShow.DayOfWeek == DayOfWeek.Monday ? "Thứ 2" :
                    dayShow.DayOfWeek == DayOfWeek.Tuesday ? "Thứ 3" :
                    dayShow.DayOfWeek == DayOfWeek.Wednesday ? "Thứ 4" :
                    dayShow.DayOfWeek == DayOfWeek.Thursday ? "Thứ 5" :
                    dayShow.DayOfWeek == DayOfWeek.Friday ? "Thứ 6" :
                    dayShow.DayOfWeek == DayOfWeek.Saturday ? "Thứ 7" : "CN") + " (" + dayShow.Day.ToString() + "/" + dayShow.Month.ToString() + ")";
                gridTemp.Columns[(i + 3)].HeaderText =
                    (dayShow.DayOfWeek == DayOfWeek.Monday ? "Thứ 2" :
                    dayShow.DayOfWeek == DayOfWeek.Tuesday ? "Thứ 3" :
                    dayShow.DayOfWeek == DayOfWeek.Wednesday ? "Thứ 4" :
                    dayShow.DayOfWeek == DayOfWeek.Thursday ? "Thứ 5" :
                    dayShow.DayOfWeek == DayOfWeek.Friday ? "Thứ 6" :
                    dayShow.DayOfWeek == DayOfWeek.Saturday ? "Thứ 7" : "CN") + " (" + dayShow.Day.ToString() + "/" + dayShow.Month.ToString() + ")";
                _Time[j] = dayShow.Day.ToString() + "/" + dayShow.Month.ToString();
                j = j + 1;
            }
        }
        public void Get_Day(DateTime StartDay, DateTime FinishDay)
        {
            _StartDay = StartDay;
            _FinishDay = FinishDay;
            int j = 0;
            string[] _Temp = StartDay.ToString().Split('/');
            int _Day = int.Parse(_Temp[1]), _Moth = int.Parse(_Temp[0]), _Year = int.Parse(_Temp[2].Remove(4));
            for (int i = 0; i < 6; i++)
            {
                // MessageBox.Show(DateTime.Parse(_Moth.ToString() + "/" + _Day.ToString() +"/"+ _Year.ToString()).DayOfWeek.ToString());
                if (DateTime.Parse(_Moth.ToString() + "/" + _Day.ToString() + "/" + _Year.ToString()).DayOfWeek.ToString() == "Sunday")
                {
                    _Day = _Day + 1;
                }
                if (_Day > this.DayofMonth(_Moth, _Year))
                {
                    _Day = 1;
                    _Moth = _Moth + 1;
                    if (_Moth > 12)
                    {
                        _Year = _Year + 1;
                    }
                }
                _Time[j] = _Day.ToString() + "/" + _Moth.ToString();
                j = j + 1;
                _Day = _Day + 1;
            }
        }
        public void ReturnDay(DateTime InputDay)
        {
            int i = 0;
            DateTime dayShown = InputDay;
            while (dayShown.DayOfWeek != DayOfWeek.Monday)
                dayShown = dayShown.AddDays(-1);

            while (dayShown.DayOfWeek != DayOfWeek.Sunday)
            {
                _Time[i] = dayShown.Day.ToString() + "/" + dayShown.Month.ToString();
                i += 1;
                dayShown = dayShown.AddDays(1);
            }
        }

        public void FillDataBase(GridView grdThongke, GridView grdTonghop)
        {
            ClsKhauphandinhduongtrongtuanDAL ClsKhauphandinhduongtrongtuandal = new ClsKhauphandinhduongtrongtuanDAL();
            int j = 0;
            DataTable _Table;
            DataTable _DataTable;
            _Table = ClsKhauphandinhduongtrongtuandal.ReturnTable(1, _StartDay, _FinishDay, _Time);
            _DataTable = ClsKhauphandinhduongtrongtuandal.ReturnTable(2, _StartDay, _FinishDay, _Time);

            //MessageBox.Show(_Table.Rows.Count.ToString() + "   " + _DataTable.Rows.Count.ToString());
            if (_Table.Rows.Count > 0)
            {
                foreach (DataRow dr in _Table.Rows)
                {
                    string[] _Temp = dr["NgayNX"].ToString().Split('/');
                    string _Ngay = (int.Parse(_Temp[0])).ToString() + "/" + (int.Parse(_Temp[1])).ToString();
                    for (int i = 0; i < _Time.Length; i++)
                    {
                        // MessageBox.Show(_Ngay.ToString() + "so sanh " + _Time[i]);
                        if (_Ngay == _Time[i])
                        {
                            if (this.CheckRow(_Source, dr["Tenthucan"].ToString()))
                            {
                                //_Source.Columns.Add("STT");
                                _Source.Rows.Add(1);
                                _Source.Rows[j]["STT"] = (j + 1).ToString();
                                _Source.Rows[j]["Tenthucan"] = dr["Tenthucan"].ToString();
                                _Source.Rows[j]["Tendonvitinh"] = dr["Tendonvitinh"].ToString();
                                _Source.Rows[j][_Ngay] = dr["Soluong"].ToString();
                                _Source.Rows[j]["nangluong"] = dr["Calo"].ToString();
                                try
                                {
                                    bool isNguyenlieu = bool.Parse(dr["Nguondongvat"].ToString());
                                    if (isNguyenlieu == false)
                                    {
                                        _Source.Rows[j]["TV1"] = dr["Tphh_Protid"].ToString();
                                        _Source.Rows[j]["TV2"] = dr["Tphh_Lipid"].ToString();
                                        _Source.Rows[j]["DV1"] = 0;
                                        _Source.Rows[j]["DV2"] = 0;
                                    }
                                    else
                                    {
                                        _Source.Rows[j]["DV1"] = dr["Tphh_Protid"].ToString();
                                        _Source.Rows[j]["DV2"] = dr["Tphh_Lipid"].ToString();
                                        _Source.Rows[j]["TV1"] = 0;
                                        _Source.Rows[j]["TV1"] = 0;
                                    }
                                }
                                catch { }
                                _Source.Rows[j]["duongbot"] = dr["Tphh_GlucId"].ToString();
                                j = j + 1;
                                break;
                            }
                            else
                            {
                                this.UpdateGridView(_Source, dr["Tenthucan"].ToString(), _Ngay, dr["Soluong"].ToString());
                            }
                        }
                    }
                }
                ClsTblDm_XuatkhoBLL ClsTblDm_Xuatkho_bll = new ClsTblDm_XuatkhoBLL();
                if (_DataTable.Rows.Count > 0)
                {
                    int m; _tongSoTre = 0;
                    foreach (DataRow dr in _DataTable.Rows)
                    {
                        if (int.TryParse(dr["Tongsosuat"].ToString(), out m))
                        {
                            //GetTable_TblXuathanghoa_GetbyNgayNX
                            if (dr["Tongsosuat"].ToString() != "0" && ClsTblDm_Xuatkho_bll.GetTable_TblXuathanghoa_GetbyNgayNX(DateTime.Parse(DateTime.Parse(dr["Ngay"].ToString()).ToShortDateString())).Rows.Count > 0)
                            {
                                _tongSoTre = _tongSoTre + int.Parse(dr["Tongsosuat"].ToString());
                            }
                        }
                    }
                }
            }
            grdThongke.GridControl.DataSource = _Source;
            Cell_EndEdit(grdTonghop, _Source);
        }
        private bool CheckRow(DataTable gridView, string Values)
        {
            if (gridView.Rows.Count > 0)
            {
                for (int i = 0; i < gridView.Rows.Count; i++)
                {
                    if (gridView.Rows[i]["Tenthucan"].ToString() == Values)
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        private void UpdateGridView(DataTable gridView, string Name, string Fields, string Values)
        {
            if (gridView.Rows.Count > 0)
            {
                for (int i = 0; i < gridView.Rows.Count; i++)
                {
                    if (gridView.Rows[i]["Tenthucan"].ToString() == Name.ToString())
                    {
                        gridView.Rows[i][Fields] = Values;
                    }
                }
            }
        }
        public void Cell_EndEdit(GridView grdView, DataTable gridView)
        {
            int[] ingaythu = new int[] { 0, 0, 0, 0, 0, 0, 0 };
            int iTongsongay = 0;
            float m, _trungGian = 0, _nangLuong = 0, _damDV = 0, _beoDV = 0, _tyLeThai = 0, _tyLeQD = 0, _duongBot = 0;
            // trong đó: _trungGian số lượng mua của thực phẩm trong tuần và bẳng tổng số lượng của 1 thực phẩm trong tuần. 
            // Nangluong = Tổng năng lượng chung tính cho các thực phẩm
            if (gridView.Rows.Count > 0)
            {
                for (int i = 0; i < gridView.Rows.Count; i++)
                {
                    for (int j = 3; j < 9; j++)
                    {
                        if (gridView.Rows[i][j] != null)
                        {
                            if (float.TryParse(gridView.Rows[i][j].ToString(), out m))
                            {
                                _trungGian = _trungGian + float.Parse(gridView.Rows[i][j].ToString());
                                ingaythu[j - 3] = 1;
                            }
                        }
                    }
                    DataTable _Table;
                    ClsTblDm_SosanhnangluonDAL ClsTblDm_Sosanhnangluondal = new ClsTblDm_SosanhnangluonDAL();
                    _Table = ClsTblDm_Sosanhnangluondal.GetTable(QueryString.PRCTBLDM_SOSANHDINHDUONG_SELECTALL1, 1, 3, gridView.Rows[i]["Tenthucan"].ToString());
                    if (_Table.Rows.Count > 0)
                    {
                        DataRow dr = _Table.Rows[0];
                        if (float.TryParse(dr["Tylethai"].ToString(), out m))
                        {
                            _tyLeThai = float.Parse(dr["Tylethai"].ToString());

                        }
                        if (float.TryParse(dr["Tylequydoi"].ToString(), out m))
                        {
                            _tyLeQD = float.Parse(dr["Tylequydoi"].ToString());
                        }

                    }
                    gridView.Rows[i]["tong"] = _trungGian.ToString();
                    _trungGian = 0;
                    if (float.TryParse(gridView.Rows[i]["tong"].ToString(), out m))
                    {
                        if (gridView.Rows[i]["nangluong"] != null)
                        {
                            if (_tyLeQD != 0)
                            {
                                try
                                {
                                    // _nangLuong = _nangLuong + ((float)((float.Parse(gridView.Rows[i].Cells["tong"].Value.ToString()) * _temp) / _tyLeQD) * float.Parse(gridView.Rows[i].Cells["nangluong"].Value.ToString())) * 10;
                                    _nangLuong = _nangLuong + float.Parse(gridView.Rows[i]["tong"].ToString()) * float.Parse(gridView.Rows[i]["nangluong"].ToString()) * 10 * (1 - _tyLeThai / 100) * 1 / _tyLeQD;
                                }
                                catch
                                {
                                }
                                //MessageBox.Show("Tong " + gridView.Rows[i].Cells["tong"].Value.ToString() + " Ty le quy doi " + _tyLeQD.ToString() + "Nhu cau nll " + gridView.Rows[i].Cells["nangluong"].Value.ToString());
                                //MessageBox.Show("Năng lượng " + _nangLuong.ToString());
                            }
                        }
                        if (gridView.Rows[i]["DV1"] != null)
                        {
                            if (_tyLeQD != 0)
                            {
                                //object obj = ;
                                //object obj1 = ;
                                // _damDV = _damDV + (((float.Parse(gridView.Rows[i].Cells["tong"].Value.ToString())) / _tyLeQD) * (float.Parse(gridView.Rows[i].Cells["DV1"].Value.ToString()) / 1000)) * 10 * 4 * (1 - _tyLeThai / 100);
                                _damDV = _damDV + float.Parse(gridView.Rows[i]["tong"].ToString()) * float.Parse(gridView.Rows[i]["DV1"].ToString()) * 10 * 4 * (1 - _tyLeThai / 100) * 1 / _tyLeQD;
                            }
                        }
                        if (gridView.Rows[i]["TV1"] != null)
                        {
                            if (_tyLeQD != 0)
                            {
                                _damDV = _damDV + float.Parse(gridView.Rows[i]["tong"].ToString()) * float.Parse(gridView.Rows[i]["TV1"].ToString()) * 10 * 4 * (1 - _tyLeThai / 100) * 1 / _tyLeQD;
                                // _damDV = _damDV + (((float.Parse(gridView.Rows[i].Cells["tong"].Value.ToString()) ) / _tyLeQD) * (float.Parse(gridView.Rows[i].Cells["TV1"].Value.ToString()) / 1000)) * 10 *4* (1 - _tyLeThai / 100);
                            }
                        }
                        if (gridView.Rows[i]["DV2"] != null)
                        {
                            if (_tyLeQD != 0)
                            {
                                _beoDV = _beoDV + float.Parse(gridView.Rows[i]["tong"].ToString()) * float.Parse(gridView.Rows[i]["DV2"].ToString()) * 10 * 9 * (1 - _tyLeThai / 100) * 1 / _tyLeQD;

                            }
                        }
                        if (gridView.Rows[i]["TV2"] != null)
                        {
                            if (_tyLeQD != 0)
                            {
                                _beoDV = _beoDV + float.Parse(gridView.Rows[i]["tong"].ToString()) * float.Parse(gridView.Rows[i]["TV2"].ToString()) * 10 * 9 * (1 - _tyLeThai / 100) * 1 / _tyLeQD;
                            }
                        }
                        if (gridView.Rows[i]["duongbot"] != null)
                        {
                            if (_tyLeQD != 0)
                            {
                                _duongBot = _duongBot + float.Parse(gridView.Rows[i]["tong"].ToString()) * float.Parse(gridView.Rows[i]["duongbot"].ToString()) * 10 * 4 * (1 - _tyLeThai / 100) * 1 / _tyLeQD;
                            }
                        }
                    }
                }
                
                TableTH.Rows.Add(2);
                TableTH.Rows[0]["Tieude"] = "Tổng cộng dưỡng chất";
                TableTH.Rows[0]["Ngangluong"] = _nangLuong.ToString("#0.00");
                TableTH.Rows[0]["dam"] = _damDV.ToString("#0.00");
                TableTH.Rows[0]["beo"] = _beoDV.ToString("#0.00");
                TableTH.Rows[0]["duong"] = _duongBot.ToString("#0.00");
                _trungGian = _nangLuong / _tongSoTre;
                foreach (int i in ingaythu)
                {
                    iTongsongay = iTongsongay + i;
                }
                //MessageBox.Show(_nangLuong.ToString());
                TableTH.Rows.Add(1);
                if (_tongSoTre == 0)
                {
                    _trungGian = 0;
                    TableTH.Rows[1]["Tieude"] = "Tỷ lệ trung bình trong ngày";
                    TableTH.Rows[1]["Ngangluong"] = 0;
                    TableTH.Rows[1]["dam"] = 0;
                    TableTH.Rows[1]["beo"] = 0;
                    TableTH.Rows[1]["duong"] = 0;
                }
                else
                {
                    TableTH.Rows[1]["Tieude"] = "Tỷ lệ trung bình trong ngày";
                    TableTH.Rows[1]["Ngangluong"] = (_nangLuong / _tongSoTre).ToString("#0.00");
                    TableTH.Rows[1]["dam"] = (_damDV / _tongSoTre).ToString("#0.00");
                    TableTH.Rows[1]["beo"] = (_beoDV / _tongSoTre).ToString("#0.00");
                    TableTH.Rows[1]["duong"] = (_duongBot / _tongSoTre).ToString("#0.00");
                }
                _trungGian = _damDV + _beoDV + _duongBot;
                TableTH.Rows.Add(2);
                TableTH.Rows[2]["Tieude"] = "P:G:L %";
                TableTH.Rows[2]["dam"] = ((_damDV / (_damDV + _beoDV + _duongBot)) * 100).ToString("#0.00") + " %";
                TableTH.Rows[2]["beo"] = ((_beoDV / (_damDV + _beoDV + _duongBot)) * 100).ToString("#0.00") + " %";
                TableTH.Rows[2]["duong"] = ((_duongBot / (_damDV + _beoDV + _duongBot)) * 100).ToString("#0.00") + " %";
            }
            grdView.GridControl.DataSource = TableTH;
        }

        #region CheckDay
        private int DayofMonth(int Month, int year)
        {
            if (Month == 2)
            {
                if ((year % 4 == 0) && (year % 4 != 0) || (year % 400 == 0))
                {
                    return 29;
                }
                else { return 28; }
            }
            else
            {
                switch (Month)
                {
                    case 1: { return 31; }
                    case 3: { return 31; }
                    case 5: { return 31; }
                    case 7: { return 31; }
                    case 8: { return 31; }
                    case 10: { return 31; }
                    case 12: { return 31; }
                    case 4: { return 30; }
                    case 6: { return 30; }
                    case 9: { return 30; }
                    case 11: { return 30; }
                    default: return 30;
                }
            }
        }
        #endregion

        #region Export file

        public void ExportFileExel(DataGridView Gird, SaveFileDialog FileSave, DateTime dateReport)
        {
            _Start = 12;
            string sTenso = null, sTentruong = null, sTenphong = null, sTieude = null, FileName = null, _Ngay = null;

            ClsTblTruonghocBLL ClsTblTruonghocbll = new ClsTblTruonghocBLL();
            DataTable Tbl = ClsTblTruonghocbll.ReturnTblTRuong();


            if (Tbl.Rows.Count > 0)
            {
                DataRow dr = Tbl.Rows[0];
                sTenso = dr["TensoGD"].ToString();
                sTentruong = dr["Tentruong"].ToString();
                sTenphong = dr["TenphongGD"].ToString();
                sTieude = "BẢNG TỔNG HỢP DINH DƯỠNG TRONG TUẦN";
                FileName = sTieude + ".xls";
                FileSave.FileName = FileName;
            }

            this.NCDDbyWeek(1, sTieude, sTenso, sTenphong, sTentruong, Gird, FileSave.FileName, dateReport);
            //this.OpenFileExel(FileName);
        }

        public override void NCDDbyWeek(int SHD, string Tieude, string Tenso, string Tenphong, string Tentruong, DataGridView Grid, string FileName, DateTime dateReport)
        {
            base.NCDDbyWeek(SHD, Tieude, Tenso, Tenphong, Tentruong, Grid, FileName, dateReport);
            rang = _xlWorkSheet.get_Range("A6", "A6");

            _xlWorkSheet.Cells[11, 12] = "Đạm";
            rang = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[11, 12], _xlWorkSheet.Cells[11, 13]);
            rang.HorizontalAlignment = 3;
            rang.BorderAround(exel.XlLineStyle.xlContinuous, exel.XlBorderWeight.xlThin, exel.XlColorIndex.xlColorIndexAutomatic, exel.XlColorIndex.xlColorIndexAutomatic);
            rang.Merge(true);

            _xlWorkSheet.Cells[11, 14] = "Béo";
            rang = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[11, 14], _xlWorkSheet.Cells[11, 15]);
            rang.HorizontalAlignment = 3;
            rang.BorderAround(exel.XlLineStyle.xlContinuous, exel.XlBorderWeight.xlThin, exel.XlColorIndex.xlColorIndexAutomatic, exel.XlColorIndex.xlColorIndexAutomatic);
            rang.Merge(true);

            for (int j = 0; j < _GridView.ColumnCount; j++)
            {
                for (int i = 0; i < _GridView.Rows.Count; i++)
                {
                    if (_GridView.Rows[i].Cells[j].Value != null)
                    {
                        if (j == _GridView.ColumnCount - 1)
                        {
                            rang = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[_Start + Grid.RowCount + 1 + i, 2 * j + 8], _xlWorkSheet.Cells[_Start + Grid.RowCount + i + 1, 2 * j + 8]);
                            rang.HorizontalAlignment = 3;
                            //rang.ColumnWidth = 10;
                            rang.BorderAround(exel.XlLineStyle.xlContinuous, exel.XlBorderWeight.xlThin, exel.XlColorIndex.xlColorIndexAutomatic, exel.XlColorIndex.xlColorIndexAutomatic);
                            _xlWorkSheet.Cells[_Start + Grid.RowCount + 1 + i, 2 * j + 8] = _GridView.Rows[i].Cells[j].Value.ToString();
                            rang = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[_Start + Grid.RowCount + 1 + i, j + 10], _xlWorkSheet.Cells[_Start + Grid.RowCount + i + 1, j + 10]);
                            rang.BorderAround(exel.XlLineStyle.xlContinuous, exel.XlBorderWeight.xlThin, exel.XlColorIndex.xlColorIndexAutomatic, exel.XlColorIndex.xlColorIndexAutomatic);
                        }
                        else
                        {
                            rang = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[_Start + Grid.RowCount + 1 + i, 2 * j + 9], _xlWorkSheet.Cells[_Start + Grid.RowCount + i + 1, 2 * j + 9]);
                            rang.HorizontalAlignment = 3;
                            //rang.ColumnWidth = 10;
                            rang.BorderAround(exel.XlLineStyle.xlContinuous, exel.XlBorderWeight.xlThin, exel.XlColorIndex.xlColorIndexAutomatic, exel.XlColorIndex.xlColorIndexAutomatic);
                            _xlWorkSheet.Cells[_Start + Grid.RowCount + 1 + i, 2 * j + 9] = _GridView.Rows[i].Cells[j].Value.ToString();
                            rang = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[_Start + Grid.RowCount + 1 + i, j + 9], _xlWorkSheet.Cells[_Start + Grid.RowCount + i + 1, j + 9]);
                            rang.BorderAround(exel.XlLineStyle.xlContinuous, exel.XlBorderWeight.xlThin, exel.XlColorIndex.xlColorIndexAutomatic, exel.XlColorIndex.xlColorIndexAutomatic);
                        }
                    }
                }
            }
            for (int i = _Start + Grid.RowCount + 1; i < _Start + _GridView.RowCount + Grid.RowCount + 1; i++)
            {
                rang = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[i, 8], _xlWorkSheet.Cells[i, 10]);
                rang.BorderAround(exel.XlLineStyle.xlContinuous, exel.XlBorderWeight.xlThin, exel.XlColorIndex.xlColorIndexAutomatic, exel.XlColorIndex.xlColorIndexAutomatic);
                rang.Merge(true);
                rang = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[i, 12], _xlWorkSheet.Cells[i, 13]);
                rang.Merge(true);
                rang = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[i, 14], _xlWorkSheet.Cells[i, 15]);
                rang.Merge(true);
            }
            try
            {
            }
            catch (System.Exception ex)
            { MessageBox.Show("Lỗi " + ex.Message); }
        }
        #endregion
        #region Code Editing
        //private string[] _Time = new string[9];
        //private DateTime _StartDay;
        //private DateTime _FinishDay;
        //private int _tongSoTre;
        //private string _Begin;
        //private string _End;
        //private AdvBandedGridView _GridView;


        //public AdvBandedGridView GridView
        //{
        //    set { _GridView = value; }
        //}
        //public int TongSoTre
        //{
        //    get { return _tongSoTre; }
        //}
        //public string Begin
        //{ get { return _Begin; } }
        //public string End
        //{ get { return _End; } }
        //public DateTime StartDay
        //{
        //    get { return _StartDay; }
        //}
        //public DateTime FinishDay
        //{ get { return _FinishDay; } }

        ////public void GetMonth(ComboBox MonthFrom, ComboBox DayFrom, TextBox ToMonth, TextBox ToDay)
        ////{
        ////    int _Month = 0, _Day = 0, _year = 0;
        ////    string[] _Temp = MonthFrom.Text.Split('/');
        ////    _Month = int.Parse(_Temp[0]); _Day = int.Parse(DayFrom.Text); _year = int.Parse(_Temp[1]);
        ////    int i = 0;
        ////    while (i < 6)
        ////    {
        ////        if (_Day >= this.DayofMonth(_Month, _year))
        ////        {
        ////            _Day = 1;
        ////            _Month = _Month + 1;
        ////            if (_Month > 12)
        ////            {
        ////                _Month = 1;
        ////                _year = _year + 1;
        ////            }
        ////            i = i + 1;
        ////        }
        ////        else
        ////        {
        ////            _Day = _Day + 1;
        ////            i = i + 1;
        ////        }

        ////    }
        ////    ToMonth.Text = _Month.ToString() + "/" + _year;
        ////    ToDay.Text = _Day.ToString();
        ////}

        //public void GetMonth(DateTime StartDay, AdvBandedGridView gridView)
        //{
        //    ReturnDay(StartDay);
        //    DateTime dayShow = StartDay;
        //    while (dayShow.DayOfWeek != DayOfWeek.Monday)
        //        dayShow = dayShow.AddDays(-1);
        //    for (int i = 0; i < 6; i++)
        //    {

        //        //gridView.Bands[(i + 3)].Name = dayShow.Day.ToString() + "/" + dayShow.Month.ToString();
        //        gridView.Bands[(i + 3)].Caption =
        //            (dayShow.DayOfWeek == DayOfWeek.Monday ? "Thứ 2" :
        //            dayShow.DayOfWeek == DayOfWeek.Tuesday ? "Thứ 3" :
        //            dayShow.DayOfWeek == DayOfWeek.Wednesday ? "Thứ 4" :
        //            dayShow.DayOfWeek == DayOfWeek.Thursday ? "Thứ 5" :
        //            dayShow.DayOfWeek == DayOfWeek.Friday ? "Thứ 6" :
        //            dayShow.DayOfWeek == DayOfWeek.Saturday ? "Thứ 7" : "CN") + " (" + dayShow.Day.ToString() + "/" + dayShow.Month.ToString() + ")";
        //        dayShow = dayShow.AddDays(1);
        //    }
        //    _Begin = _Time[0]; _End = _Time[5];
        //    string[] _Temp;
        //    _Temp = _Time[0].Split('/');
        //    _StartDay = DateTime.Parse(_Temp[1] + "/" + _Temp[0]);
        //    _Temp = _Time[5].Split('/');
        //    _FinishDay = _StartDay.AddDays(5); //DateTime.Parse(_Temp[1] + "/" + _Temp[0]);
        //}
        //public void GetMonth(DateTime StartDay, DateTime FinishDay, Label TuNgay, Label DenNgay, DataGridView gridView)
        //{
        //    _StartDay = StartDay;
        //    _FinishDay = FinishDay;
        //    int j = 0;
        //    int cSunday = 0;
        //    for (int i = 0; i < 6; i++)
        //    {
        //        DateTime dayShow = StartDay.AddDays(i + cSunday);
        //        if (dayShow.DayOfWeek == DayOfWeek.Sunday)
        //        {
        //            cSunday += 1;
        //            dayShow = dayShow.AddDays(1);
        //        }
        //        gridView.Columns[(i + 3)].Name = dayShow.Day.ToString() + "/" + dayShow.Month.ToString();
        //        gridView.Columns[(i + 3)].HeaderText =
        //            (dayShow.DayOfWeek == DayOfWeek.Monday ? "Thứ 2" :
        //            dayShow.DayOfWeek == DayOfWeek.Tuesday ? "Thứ 3" :
        //            dayShow.DayOfWeek == DayOfWeek.Wednesday ? "Thứ 4" :
        //            dayShow.DayOfWeek == DayOfWeek.Thursday ? "Thứ 5" :
        //            dayShow.DayOfWeek == DayOfWeek.Friday ? "Thứ 6" :
        //            dayShow.DayOfWeek == DayOfWeek.Saturday ? "Thứ 7" : "CN") + " (" + dayShow.Day.ToString() + "/" + dayShow.Month.ToString() + ")";
        //        _Time[j] = dayShow.Day.ToString() + "/" + dayShow.Month.ToString();
        //        j = j + 1;
        //    }
        //}
        //public void Get_Day(DateTime StartDay, DateTime FinishDay)
        //{
        //    _StartDay = StartDay;
        //    _FinishDay = FinishDay;
        //    int j = 0;
        //    string[] _Temp = StartDay.ToString().Split('/');
        //    int _Day = int.Parse(_Temp[1]), _Moth = int.Parse(_Temp[0]), _Year = int.Parse(_Temp[2].Remove(4));
        //    for (int i = 0; i < 6; i++)
        //    {
        //        // MessageBox.Show(DateTime.Parse(_Moth.ToString() + "/" + _Day.ToString() +"/"+ _Year.ToString()).DayOfWeek.ToString());
        //        if (DateTime.Parse(_Moth.ToString() + "/" + _Day.ToString() + "/" + _Year.ToString()).DayOfWeek.ToString() == "Sunday")
        //        {
        //            _Day = _Day + 1;
        //        }
        //        if (_Day > this.DayofMonth(_Moth, _Year))
        //        {
        //            _Day = 1;
        //            _Moth = _Moth + 1;
        //            if (_Moth > 12)
        //            {
        //                _Year = _Year + 1;
        //            }
        //        }
        //        _Time[j] = _Day.ToString() + "/" + _Moth.ToString();
        //        j = j + 1;
        //        _Day = _Day + 1;
        //    }
        //}
        //public void ReturnDay(DateTime InputDay)
        //{
        //    int i = 0;
        //    DateTime dayShown = InputDay;
        //    while (dayShown.DayOfWeek != DayOfWeek.Monday)
        //        dayShown = dayShown.AddDays(-1);

        //    while (dayShown.DayOfWeek != DayOfWeek.Sunday)
        //    {
        //        _Time[i] = dayShown.Day.ToString() + "/" + dayShown.Month.ToString();
        //        i += 1;
        //        dayShown = dayShown.AddDays(1);
        //    }
        //}

        //public void FillDataBase(AdvBandedGridView gridView)
        //{
        //    ClsKhauphandinhduongtrongtuanDAL ClsKhauphandinhduongtrongtuandal = new ClsKhauphandinhduongtrongtuanDAL();
        //    int j = 0;
        //    DataTable _Table;
        //    DataTable _DataTable;
        //    _Table = ClsKhauphandinhduongtrongtuandal.ReturnTable(1, _StartDay, _FinishDay, _Time);
        //    _DataTable = ClsKhauphandinhduongtrongtuandal.ReturnTable(2, _StartDay, _FinishDay, _Time);

        //    //MessageBox.Show(_Table.Rows.Count.ToString() + "   " + _DataTable.Rows.Count.ToString());
        //    if (_Table.Rows.Count > 0)
        //    {
        //        foreach (DataRow dr in _Table.Rows)
        //        {
        //            string[] _Temp = dr["NgayNX"].ToString().Split('/');
        //            string _Ngay = (int.Parse(_Temp[1])).ToString() + "/" + (int.Parse(_Temp[0])).ToString();
        //            for (int i = 0; i < _Time.Length; i++)
        //            {
        //                // MessageBox.Show(_Ngay.ToString() + "so sanh " + _Time[i]);
        //                if (_Ngay == _Time[i])
        //                {
        //                    if (this.CheckRow(gridView, dr["Tenthucan"].ToString()))
        //                    {
        //                        ((DataTable)gridView.GridControl.DataSource).Rows.Add(1);
        //                        //((DataTable)gridView.GridControl.DataSource).Rows[j][]
        //                        gridView.SetRowCellValue(j, "STT", (j + 1).ToString());
        //                        gridView.SetRowCellValue(j, "Tenthucan", dr["Tenthucan"].ToString());
        //                        gridView.SetRowCellValue(j, "Tendonvitinh", dr["Tendonvitinh"].ToString());
        //                        gridView.SetRowCellValue(j, _Ngay, dr["Soluong"].ToString());
        //                        gridView.SetRowCellValue(j, "nangluong", dr["Calo"].ToString());
        //                        //gridView.Rows.Add(1);
        //                        //gridView.Rows[j].Cells["STT"].Value = (j + 1).ToString();
        //                        //gridView.Rows[j].Cells["Tenthucan"].Value = dr["Tenthucan"].ToString();
        //                        //gridView.Rows[j].Cells["Tendonvitinh"].Value = dr["Tendonvitinh"].ToString();
        //                        //gridView.Rows[j].Cells[_Ngay].Value = dr["Soluong"].ToString();
        //                        //gridView.Rows[j].Cells["nangluong"].Value = dr["Calo"].ToString();
        //                        try
        //                        {
        //                            bool isNguyenlieu = bool.Parse(dr["Nguondongvat"].ToString());
        //                            if (isNguyenlieu == false)
        //                            {
        //                                gridView.SetRowCellValue(j, "TV1", dr["Tphh_Protid"].ToString());
        //                                gridView.SetRowCellValue(j, "TV2", dr["Tphh_Lipid"].ToString());
        //                                //gridView.Rows[j].Cells["TV1"].Value = dr["Tphh_Protid"].ToString();
        //                                //gridView.Rows[j].Cells["TV2"].Value = dr["Tphh_Lipid"].ToString();
        //                            }
        //                            else
        //                            {
        //                                gridView.SetRowCellValue(j, "DV1", dr["Tphh_Protid"].ToString());
        //                                gridView.SetRowCellValue(j, "DV2", dr["Tphh_Lipid"].ToString());
        //                                //gridView.Rows[j].Cells["DV1"].Value = dr["Tphh_Protid"].ToString();
        //                                //gridView.Rows[j].Cells["DV2"].Value = dr["Tphh_Lipid"].ToString();
        //                            }
        //                        }
        //                        catch { }
        //                        gridView.SetRowCellValue(j, "duongbot", dr["Tphh_GlucId"].ToString());
        //                        //gridView.Rows[j].Cells["duongbot"].Value = dr["Tphh_GlucId"].ToString();
        //                        j = j + 1;
        //                        break;
        //                    }
        //                    else
        //                    {
        //                        this.UpdateGridView(gridView, dr["Tenthucan"].ToString(), _Ngay, dr["Soluong"].ToString());
        //                    }
        //                }
        //            }
        //        }
        //        ClsTblDm_XuatkhoBLL ClsTblDm_Xuatkho_bll = new ClsTblDm_XuatkhoBLL();
        //        if (_DataTable.Rows.Count > 0)
        //        {
        //            int m; _tongSoTre = 0;
        //            foreach (DataRow dr in _DataTable.Rows)
        //            {
        //                if (int.TryParse(dr["Tongsosuat"].ToString(), out m))
        //                {
        //                    //GetTable_TblXuathanghoa_GetbyNgayNX
        //                    if (dr["Tongsosuat"].ToString() != "0" && ClsTblDm_Xuatkho_bll.GetTable_TblXuathanghoa_GetbyNgayNX(DateTime.Parse(DateTime.Parse(dr["Ngay"].ToString()).ToString("MM/dd/yyyy"))).Rows.Count > 0)
        //                    {
        //                        _tongSoTre = _tongSoTre + int.Parse(dr["Tongsosuat"].ToString());
        //                    }
        //                }
        //            }
        //        }
        //    }
        //}
        //private bool CheckRow(AdvBandedGridView gridView, string Values)
        //{
        //    if (gridView.RowCount > 0)
        //    {
        //        for (int i = 0; i < gridView.RowCount; i++)
        //        {
        //            if (gridView.GetRowCellValue(i, "Tenthucan").ToString() == Values)
        //            {
        //                return false;
        //            }
        //        }
        //    }
        //    return true;
        //}
        //private void UpdateGridView(AdvBandedGridView gridView, string Name, string Fields, string Values)
        //{
        //    if (gridView.RowCount > 0)
        //    {
        //        for (int i = 0; i < gridView.RowCount; i++)
        //        {
        //            if (gridView.GetRowCellValue(i, "Tenthucan").ToString() == Name.ToString())
        //            {
        //                gridView.SetRowCellValue(i, Fields, Values);
        //            }
        //        }
        //    }
        //}
        //public void Cell_EndEdit(DataGridView gridView)
        //{
        //    int[] ingaythu = new int[] { 0, 0, 0, 0, 0, 0, 0 };
        //    int iTongsongay = 0;
        //    float m, _trungGian = 0, _nangLuong = 0, _damDV = 0, _beoDV = 0, _tyLeThai = 0, _tyLeQD = 0, _duongBot = 0;
        //    // trong đó: _trungGian số lượng mua của thực phẩm trong tuần và bẳng tổng số lượng của 1 thực phẩm trong tuần. 
        //    // Nangluong = Tổng năng lượng chung tính cho các thực phẩm
        //    if (gridView.Rows.Count > 0)
        //    {
        //        for (int i = 0; i < gridView.RowCount; i++)
        //        {
        //            for (int j = 3; j < 9; j++)
        //            {
        //                if (gridView.Rows[i].Cells[j].Value != null)
        //                {
        //                    if (float.TryParse(gridView.Rows[i].Cells[j].Value.ToString(), out m))
        //                    {
        //                        _trungGian = _trungGian + float.Parse(gridView.Rows[i].Cells[j].Value.ToString());
        //                        ingaythu[j - 3] = 1;
        //                    }
        //                }
        //            }
        //            DataTable _Table;
        //            ClsTblDm_SosanhnangluonDAL ClsTblDm_Sosanhnangluondal = new ClsTblDm_SosanhnangluonDAL();
        //            _Table = ClsTblDm_Sosanhnangluondal.GetTable(QueryString.PRCTBLDM_SOSANHDINHDUONG_SELECTALL1, 1, 3, gridView.Rows[i].Cells["Tenthucan"].Value.ToString());
        //            if (_Table.Rows.Count > 0)
        //            {
        //                DataRow dr = _Table.Rows[0];
        //                if (float.TryParse(dr["Tylethai"].ToString(), out m))
        //                {
        //                    _tyLeThai = float.Parse(dr["Tylethai"].ToString());

        //                }
        //                if (float.TryParse(dr["Tylequydoi"].ToString(), out m))
        //                {
        //                    _tyLeQD = float.Parse(dr["Tylequydoi"].ToString());
        //                }

        //            }
        //            gridView.Rows[i].Cells["tong"].Value = _trungGian.ToString();
        //            _trungGian = 0;
        //            if (float.TryParse(gridView.Rows[i].Cells["tong"].Value.ToString(), out m))
        //            {
        //                if (gridView.Rows[i].Cells["nangluong"].Value != null)
        //                {
        //                    if (_tyLeQD != 0)
        //                    {
        //                        try
        //                        {
        //                            // _nangLuong = _nangLuong + ((float)((float.Parse(gridView.Rows[i].Cells["tong"].Value.ToString()) * _temp) / _tyLeQD) * float.Parse(gridView.Rows[i].Cells["nangluong"].Value.ToString())) * 10;
        //                            _nangLuong = _nangLuong + float.Parse(gridView.Rows[i].Cells["tong"].Value.ToString()) * float.Parse(gridView.Rows[i].Cells["nangluong"].Value.ToString()) * 10 * (1 - _tyLeThai / 100) * 1 / _tyLeQD;
        //                        }
        //                        catch
        //                        {
        //                        }
        //                        //MessageBox.Show("Tong " + gridView.Rows[i].Cells["tong"].Value.ToString() + " Ty le quy doi " + _tyLeQD.ToString() + "Nhu cau nll " + gridView.Rows[i].Cells["nangluong"].Value.ToString());
        //                        //MessageBox.Show("Năng lượng " + _nangLuong.ToString());
        //                    }
        //                }
        //                if (gridView.Rows[i].Cells["DV1"].Value != null)
        //                {
        //                    if (_tyLeQD != 0)
        //                    {
        //                        // _damDV = _damDV + (((float.Parse(gridView.Rows[i].Cells["tong"].Value.ToString())) / _tyLeQD) * (float.Parse(gridView.Rows[i].Cells["DV1"].Value.ToString()) / 1000)) * 10 * 4 * (1 - _tyLeThai / 100);
        //                        _damDV = _damDV + float.Parse(gridView.Rows[i].Cells["tong"].Value.ToString()) * float.Parse(gridView.Rows[i].Cells["DV1"].Value.ToString()) * 10 * 4 * (1 - _tyLeThai / 100) * 1 / _tyLeQD;
        //                    }
        //                }
        //                if (gridView.Rows[i].Cells["TV1"].Value != null)
        //                {
        //                    if (_tyLeQD != 0)
        //                    {
        //                        _damDV = _damDV + float.Parse(gridView.Rows[i].Cells["tong"].Value.ToString()) * float.Parse(gridView.Rows[i].Cells["TV1"].Value.ToString()) * 10 * 4 * (1 - _tyLeThai / 100) * 1 / _tyLeQD;
        //                        // _damDV = _damDV + (((float.Parse(gridView.Rows[i].Cells["tong"].Value.ToString()) ) / _tyLeQD) * (float.Parse(gridView.Rows[i].Cells["TV1"].Value.ToString()) / 1000)) * 10 *4* (1 - _tyLeThai / 100);
        //                    }
        //                }
        //                if (gridView.Rows[i].Cells["DV2"].Value != null)
        //                {
        //                    if (_tyLeQD != 0)
        //                    {
        //                        _beoDV = _beoDV + float.Parse(gridView.Rows[i].Cells["tong"].Value.ToString()) * float.Parse(gridView.Rows[i].Cells["DV2"].Value.ToString()) * 10 * 9 * (1 - _tyLeThai / 100) * 1 / _tyLeQD;

        //                    }
        //                }
        //                if (gridView.Rows[i].Cells["TV2"].Value != null)
        //                {
        //                    if (_tyLeQD != 0)
        //                    {
        //                        _beoDV = _beoDV + float.Parse(gridView.Rows[i].Cells["tong"].Value.ToString()) * float.Parse(gridView.Rows[i].Cells["TV2"].Value.ToString()) * 10 * 9 * (1 - _tyLeThai / 100) * 1 / _tyLeQD;
        //                    }
        //                }
        //                if (gridView.Rows[i].Cells["duongbot"].Value != null)
        //                {
        //                    if (_tyLeQD != 0)
        //                    {
        //                        _duongBot = _duongBot + float.Parse(gridView.Rows[i].Cells["tong"].Value.ToString()) * float.Parse(gridView.Rows[i].Cells["duongbot"].Value.ToString()) * 10 * 4 * (1 - _tyLeThai / 100) * 1 / _tyLeQD;
        //                    }
        //                }
        //            }
        //        }
        //        _GridView.Rows.Add(2);
        //        _GridView.Rows[0].Cells["Tieude"].Value = "Tổng cộng dưỡng chất";
        //        _GridView.Rows[0].Cells["Ngangluong"].Value = _nangLuong.ToString("#0.00");
        //        _GridView.Rows[0].Cells["dam"].Value = _damDV.ToString("#0.00");
        //        _GridView.Rows[0].Cells["beo"].Value = _beoDV.ToString("#0.00");
        //        _GridView.Rows[0].Cells["duong"].Value = _duongBot.ToString("#0.00");
        //        _trungGian = _nangLuong / _tongSoTre;
        //        foreach (int i in ingaythu)
        //        {
        //            iTongsongay = iTongsongay + i;
        //        }
        //        //MessageBox.Show(_nangLuong.ToString());
        //        _GridView.Rows.Add(1);
        //        if (_tongSoTre == 0)
        //        {
        //            _trungGian = 0;
        //            _GridView.Rows[1].Cells["Tieude"].Value = "Tỷ lệ trung bình trong ngày";
        //            _GridView.Rows[1].Cells["Ngangluong"].Value = 0;
        //            _GridView.Rows[1].Cells["dam"].Value = 0;
        //            _GridView.Rows[1].Cells["beo"].Value = 0;
        //            _GridView.Rows[1].Cells["duong"].Value = 0;
        //        }
        //        else
        //        {
        //            _GridView.Rows[1].Cells["Tieude"].Value = "Tỷ lệ trung bình trong ngày";
        //            _GridView.Rows[1].Cells["Ngangluong"].Value = (_nangLuong / _tongSoTre).ToString("#0.00");
        //            _GridView.Rows[1].Cells["dam"].Value = (_damDV / _tongSoTre).ToString("#0.00");
        //            _GridView.Rows[1].Cells["beo"].Value = (_beoDV / _tongSoTre).ToString("#0.00");
        //            _GridView.Rows[1].Cells["duong"].Value = (_duongBot / _tongSoTre).ToString("#0.00");
        //        }
        //        _trungGian = _damDV + _beoDV + _duongBot;
        //        _GridView.Rows[2].Cells["Tieude"].Value = "P:G:L %";
        //        _GridView.Rows[2].Cells["dam"].Value = ((_damDV / (_damDV + _beoDV + _duongBot)) * 100).ToString("#0.00") + " %";
        //        _GridView.Rows[2].Cells["beo"].Value = ((_beoDV / (_damDV + _beoDV + _duongBot)) * 100).ToString("#0.00") + " %";
        //        _GridView.Rows[2].Cells["duong"].Value = ((_duongBot / (_damDV + _beoDV + _duongBot)) * 100).ToString("#0.00") + " %";
        //    }
        //}

        //#region CheckDay
        //private int DayofMonth(int Month, int year)
        //{
        //    if (Month == 2)
        //    {
        //        if ((year % 4 == 0) && (year % 4 != 0) || (year % 400 == 0))
        //        {
        //            return 29;
        //        }
        //        else { return 28; }
        //    }
        //    else
        //    {
        //        switch (Month)
        //        {
        //            case 1: { return 31; }
        //            case 3: { return 31; }
        //            case 5: { return 31; }
        //            case 7: { return 31; }
        //            case 8: { return 31; }
        //            case 10: { return 31; }
        //            case 12: { return 31; }
        //            case 4: { return 30; }
        //            case 6: { return 30; }
        //            case 9: { return 30; }
        //            case 11: { return 30; }
        //            default: return 30;
        //        }
        //    }
        //}
        //#endregion

        //#region Export file

        //public void ExportFileExel(DataGridView Gird, SaveFileDialog FileSave, DateTime dateReport)
        //{
        //    _Start = 12;
        //    string sTenso = null, sTentruong = null, sTenphong = null, sTieude = null, FileName = null, _Ngay = null;

        //    ClsTblTruonghocBLL ClsTblTruonghocbll = new ClsTblTruonghocBLL();
        //    DataTable Tbl = ClsTblTruonghocbll.ReturnTblTRuong();


        //    if (Tbl.Rows.Count > 0)
        //    {
        //        DataRow dr = Tbl.Rows[0];
        //        sTenso = dr["TensoGD"].ToString();
        //        sTentruong = dr["Tentruong"].ToString();
        //        sTenphong = dr["TenphongGD"].ToString();
        //        sTieude = "BẢNG TỔNG HỢP DINH DƯỠNG TRONG TUẦN";
        //        FileName = sTieude + ".xls";
        //        FileSave.FileName = FileName;
        //    }

        //    this.NCDDbyWeek(1, sTieude, sTenso, sTenphong, sTentruong, Gird, FileSave.FileName, dateReport);
        //    //this.OpenFileExel(FileName);
        //}

        //public override void NCDDbyWeek(int SHD, string Tieude, string Tenso, string Tenphong, string Tentruong, DataGridView Grid, string FileName, DateTime dateReport)
        //{
        //    base.NCDDbyWeek(SHD, Tieude, Tenso, Tenphong, Tentruong, Grid, FileName, dateReport);
        //    rang = _xlWorkSheet.get_Range("A6", "A6");

        //    _xlWorkSheet.Cells[11, 12] = "Đạm";
        //    rang = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[11, 12], _xlWorkSheet.Cells[11, 13]);
        //    rang.HorizontalAlignment = 3;
        //    rang.BorderAround(exel.XlLineStyle.xlContinuous, exel.XlBorderWeight.xlThin, exel.XlColorIndex.xlColorIndexAutomatic, exel.XlColorIndex.xlColorIndexAutomatic);
        //    rang.Merge(true);

        //    _xlWorkSheet.Cells[11, 14] = "Béo";
        //    rang = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[11, 14], _xlWorkSheet.Cells[11, 15]);
        //    rang.HorizontalAlignment = 3;
        //    rang.BorderAround(exel.XlLineStyle.xlContinuous, exel.XlBorderWeight.xlThin, exel.XlColorIndex.xlColorIndexAutomatic, exel.XlColorIndex.xlColorIndexAutomatic);
        //    rang.Merge(true);
        //    ////for (int i = 1; i < 17; i++)
        //    ////{
        //    ////    rang = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[11, i], _xlWorkSheet.Cells[11, i]);
        //    ////    rang.Font.Name = "Times New Roman";
        //    ////    rang.Font.Size = "12";
        //    ////    //rang.HorizontalAlignment = 3;
        //    ////    //rang.ColumnWidth = 10;               
        //    ////    rang.BorderAround(exel.XlLineStyle.xlContinuous, exel.XlBorderWeight.xlThin, exel.XlColorIndex.xlColorIndexAutomatic, exel.XlColorIndex.xlColorIndexAutomatic);
        //    ////}
        //    //////_xlWorkSheet.Cells[11, 2] = "Thứ ";
        //    //////_xlWorkSheet.Cells[11, 4] = "Thứ hai";
        //    //////_xlWorkSheet.Cells[11, 5] = "Thứ ba";
        //    //////_xlWorkSheet.Cells[11, 6] = "Thứ tư";
        //    //////_xlWorkSheet.Cells[11, 7] = "Thứ năm";
        //    //////_xlWorkSheet.Cells[11, 8] = "Thứ sáu";
        //    //////_xlWorkSheet.Cells[11, 9] = "Thứ bảy";

        //    ////// Thông tin khác

        //    for (int j = 0; j < _GridView.ColumnCount; j++)
        //    {
        //        for (int i = 0; i < _GridView.Rows.Count; i++)
        //        {
        //            if (_GridView.Rows[i].Cells[j].Value != null)
        //            {
        //                if (j == _GridView.ColumnCount - 1)
        //                {
        //                    rang = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[_Start + Grid.RowCount + 1 + i, 2 * j + 8], _xlWorkSheet.Cells[_Start + Grid.RowCount + i + 1, 2 * j + 8]);
        //                    rang.HorizontalAlignment = 3;
        //                    //rang.ColumnWidth = 10;
        //                    rang.BorderAround(exel.XlLineStyle.xlContinuous, exel.XlBorderWeight.xlThin, exel.XlColorIndex.xlColorIndexAutomatic, exel.XlColorIndex.xlColorIndexAutomatic);
        //                    _xlWorkSheet.Cells[_Start + Grid.RowCount + 1 + i, 2 * j + 8] = _GridView.Rows[i].Cells[j].Value.ToString();
        //                    rang = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[_Start + Grid.RowCount + 1 + i, j + 10], _xlWorkSheet.Cells[_Start + Grid.RowCount + i + 1, j + 10]);
        //                    rang.BorderAround(exel.XlLineStyle.xlContinuous, exel.XlBorderWeight.xlThin, exel.XlColorIndex.xlColorIndexAutomatic, exel.XlColorIndex.xlColorIndexAutomatic);
        //                }
        //                else
        //                {
        //                    rang = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[_Start + Grid.RowCount + 1 + i, 2 * j + 9], _xlWorkSheet.Cells[_Start + Grid.RowCount + i + 1, 2 * j + 9]);
        //                    rang.HorizontalAlignment = 3;
        //                    //rang.ColumnWidth = 10;
        //                    rang.BorderAround(exel.XlLineStyle.xlContinuous, exel.XlBorderWeight.xlThin, exel.XlColorIndex.xlColorIndexAutomatic, exel.XlColorIndex.xlColorIndexAutomatic);
        //                    _xlWorkSheet.Cells[_Start + Grid.RowCount + 1 + i, 2 * j + 9] = _GridView.Rows[i].Cells[j].Value.ToString();
        //                    rang = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[_Start + Grid.RowCount + 1 + i, j + 9], _xlWorkSheet.Cells[_Start + Grid.RowCount + i + 1, j + 9]);
        //                    rang.BorderAround(exel.XlLineStyle.xlContinuous, exel.XlBorderWeight.xlThin, exel.XlColorIndex.xlColorIndexAutomatic, exel.XlColorIndex.xlColorIndexAutomatic);
        //                }
        //            }
        //        }
        //    }
        //    for (int i = _Start + Grid.RowCount + 1; i < _Start + _GridView.RowCount + Grid.RowCount + 1; i++)
        //    {
        //        rang = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[i, 8], _xlWorkSheet.Cells[i, 10]);
        //        rang.BorderAround(exel.XlLineStyle.xlContinuous, exel.XlBorderWeight.xlThin, exel.XlColorIndex.xlColorIndexAutomatic, exel.XlColorIndex.xlColorIndexAutomatic);
        //        rang.Merge(true);
        //        rang = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[i, 12], _xlWorkSheet.Cells[i, 13]);
        //        rang.Merge(true);
        //        rang = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[i, 14], _xlWorkSheet.Cells[i, 15]);
        //        rang.Merge(true);
        //    }
        //    try
        //    {
        //        //_xlWorkBook.SaveAs(FileName, exel.XlFileFormat.xlWorkbookNormal, Value, Value, Value, Value, exel.XlSaveAsAccessMode.xlExclusive, Value, Value, Value, Value, Value);
        //        //_xlWorkBook.Close(true, Value, Value);                    
        //        //_xlWorkSheet = (Excel.Worksheet)_xlWorkBook.Worksheets.get_Item(1);            
        //    }
        //    catch (System.Exception ex)
        //    { MessageBox.Show("Lỗi " + ex.Message); }
        //    ////   // _xlWorkBook = _exlApplication.Workbooks.Open(FileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        //}
        #endregion
    }
}
